/**   
 * @Title: MainActivityHealthTestFragment.java 
 * @Package cn.edu.scu.healthclient.activity.fragment 
 * @Description: TODO 
 * @author 
 * @date 
 * @version V0.0   
 */
package cn.edu.cdtu.healthclient.activity.fragment;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import cn.edu.cdtu.healthclient.activity.MainActivity;
import cn.edu.cdtu.healthclient.activity.TestActivity;
import cn.edu.cdtu.healthclient.domain.TestType;
import cn.edu.cdtu.healthclient.utils.ConnectUtil.HealthServerContect;
import cn.edu.cdtu.healthclient.utils.ConnectUtil.HealthTestPargrams;
import cn.edu.scu.healthclient.activity.R;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/** 
 * @ClassName: MainActivityHealthTestFragment 
 * @Description: TODO
 * @author 
 * @date 
 *  
 */
public class MainActivityHealthTestFragment extends Fragment {

	private TextView topTitle;
	private ListView listView;
	
	/* (non-Javadoc)
	 * <p>Title: onActivityCreated</p> 
	 * <p>Description: </p> 
	 * @param savedInstanceState 
	 * @see android.support.v4.app.Fragment#onActivityCreated(android.os.Bundle)
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		topTitle = (TextView) getView().findViewById(R.id.topTitle);  
	    topTitle.setText("健康测试");
	    listView = (ListView)getView().findViewById(R.id.health_test_type_list);
	}
	
	/* (non-Javadoc)
	 * <p>Title: onStart</p> 
	 * <p>Description: </p>  
	 * @see android.support.v4.app.Fragment#onStart()
	 */
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		((MainActivity)getActivity()).getTaskQueue().addTask(
	    		new Thread(new TestTypeTaskImplementation()));
		super.onStart();
	}

	/* (non-Javadoc)
	 * <p>Title: onCreateView</p> 
	 * <p>Description: </p> 
	 * @param inflater
	 * @param container
	 * @param savedInstanceState
	 * @return 
	 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(
				R.layout.activity_main_healthtest_fragment, container, false); 
	}
	/**
	 * 
	 * @ClassName: TestTypeTaskImplementation 
	 * @Description: TODO
	 * @author 
	 * @date 
	 *
	 */
	private class TestTypeTaskImplementation implements Runnable{

		/* (non-Javadoc)
		 * <p>Title: run</p> 
		 * <p>Description: </p>  
		 * @see java.lang.Runnable#run()
		 */
		@Override
		public void run() {
			// TODO Auto-generated method stub
			HttpClient httpClient = 
					((MainActivity)getActivity()).getBinder().getHttpClient();
			HttpPost httpPost = new HttpPost(HealthServerContect.SERVER_URL
					+ HealthServerContect.TEST_TYPE_SERVLET);
			List<NameValuePair> postParams = new ArrayList<NameValuePair>();
			postParams.add(new BasicNameValuePair(
					HealthTestPargrams.Option.HEALTH_TEST_OPTION,
					HealthTestPargrams.Option.OptionValue.HEALTH_TEST_OPTION_GET_TEST_TYPE));
			try {
				httpPost.setEntity(new UrlEncodedFormEntity(postParams,"utf-8"));
				HttpResponse response = httpClient.execute(httpPost);
				if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					String message = EntityUtils.toString(response.getEntity());
					Gson gson = new Gson();
					final List<TestType> types = gson.fromJson(message, 
							new TypeToken<List<TestType>>(){}.getType());
					getActivity().runOnUiThread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							listView.setAdapter(new TestTypeAdapter(types));
						}
					});
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	/**
	 * 
	 * @ClassName: TestTypeAdapter 
	 * @Description: TODO
	 * @author 
	 * @date 
	 *
	 */
	private class TestTypeAdapter extends BaseAdapter{

		private TestType[] testTypes;
		
		/** 
		 * @Title: FragmentSearch.TestTypeAdapter
		 * @Description: TODO
		 * @throws 
		 *
		 */
		public TestTypeAdapter(List<TestType> testTypeList) {
			// TODO Auto-generated constructor stub
			testTypes = testTypeList.toArray(new TestType[testTypeList.size()]);
		}
		
		/* (non-Javadoc)
		 * <p>Title: getCount</p> 
		 * <p>Description: </p> 
		 * @return 
		 * @see android.widget.Adapter#getCount()
		 */
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return testTypes.length;
		}

		/* (non-Javadoc)
		 * <p>Title: getItem</p> 
		 * <p>Description: </p> 
		 * @param position
		 * @return 
		 * @see android.widget.Adapter#getItem(int)
		 */
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		/* (non-Javadoc)
		 * <p>Title: getItemId</p> 
		 * <p>Description: </p> 
		 * @param position
		 * @return 
		 * @see android.widget.Adapter#getItemId(int)
		 */
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		/**
		 * 
		 * @ClassName: ViewHolder 
		 * @Description: Adapter的View Holder机制用到的Holder
		 * @author 
		 * @date 
		 *
		 */
		private final class ViewHolder{
			TextView typeName;
			TextView typeDescription;
			Button seeTest;
		}
		/* (non-Javadoc)
		 * <p>Title: getView</p> 
		 * <p>Description: </p> 
		 * @param position
		 * @param convertView
		 * @param parent
		 * @return 
		 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
		 */
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder viewHolder = null;
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = ((LayoutInflater)getActivity().getSystemService(
						Context.LAYOUT_INFLATER_SERVICE)).inflate(
						R.layout.activity_main_healthnews_fragment_test_type_adapter,
						parent,false);
				viewHolder.typeName = (TextView)convertView.findViewById(R.id.test_type_name);
				viewHolder.typeDescription = 
						(TextView)convertView.findViewById(R.id.test_type_descrption);
				viewHolder.seeTest = (Button) convertView.findViewById(
						R.id.get_test_by_test_type);
				convertView.setTag(viewHolder);
			}else{
				viewHolder = (ViewHolder)convertView.getTag();
			}
			
			if(position == 0){
				viewHolder.typeName.setText("心理健康测试");
				viewHolder.typeDescription.setText("心理健康测试描述\r\n心理健康测试描述\r\n心理健康测试描述\r\n心理健康测试描述\r\n心理健康测试描述");
			}else if(position == 1){
				viewHolder.typeName.setText("身体健康测试");
				viewHolder.typeDescription.setText("身体健康测试描述\r\n身体健康测试描述\r\n身体健康测试描述\r\n身体健康测试描述\r\n身体健康测试描述");
				
			}
			
			viewHolder.seeTest.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Bundle bundle = new Bundle();
					bundle.putSerializable(
							HealthTestPargrams.Pargram.SELECT_TEST_TYPE
							, testTypes[position]);
					Intent intent = new Intent(getActivity(),TestActivity.class);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			});
			
			return convertView;
		}
		
	}
}
